package com.example.service.other;


import com.example.mapper.appointment.UserMapper;
import com.example.mapper.medical.DoctorMapper;
import com.example.mapper.other.ChatRoleMapper;
import com.example.mapper.shopping.BusinessMapper;
import com.example.model.entity.appointment.User;
import com.example.model.entity.medical.Doctor;
import com.example.model.entity.other.ChatRole;
import com.example.model.entity.shopping.Business;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 角色聊天表业务处理
 **/
@Service
public class ChatRoleService {

    @Resource
    private ChatRoleMapper chatRoleMapper;
    @Resource
    private DoctorMapper doctorMapper;
    @Resource
    private UserMapper userMapper;
    @Resource
    private BusinessMapper businessMapper;


    /**
     * 新增
     */
    public Map<String, Integer> add(ChatRole chatRole) {
        // 创建两个 ChatRole 对象
        ChatRole chatRole1 = new ChatRole();
        ChatRole chatRole2 = new ChatRole();
        Business business;
        Doctor doctor;
        ChatRole existingRole1;
        if(chatRole.getBusinessId()!=null){
             business = businessMapper.selectById(chatRole.getBusinessId());
            // 设置 chatRole1 属性
            chatRole1.setName(business.getName());
            chatRole1.setAvatar(business.getAvatar());
            chatRole1.setRole(business.getRole());
            chatRole1.setBusinessId(business.getId());
            // 检查 chatRole1 是否存在
            existingRole1 = chatRoleMapper.selectByBusinessId(business.getId());
            if (existingRole1 == null) {
                chatRoleMapper.insert(chatRole1);
                existingRole1 =  chatRoleMapper.selectByBusinessId(business.getId());
            }
        }else {
             doctor = doctorMapper.selectById(chatRole.getChatUserId());
            // 设置 chatRole1 属性
            chatRole1.setName(doctor.getName());
            chatRole1.setAvatar(doctor.getAvatar());
            chatRole1.setRole(doctor.getRole());
            chatRole1.setDoctorId(doctor.getId());
            // 检查 chatRole1 是否存在
             existingRole1 = chatRoleMapper.selectByDoctorId(doctor.getId());
            if (existingRole1 == null) {
                chatRoleMapper.insert(chatRole1);
                existingRole1 = chatRoleMapper.selectByDoctorId(doctor.getId());
            }
        }
        User user = userMapper.selectById(chatRole.getUserId());
        // 设置 chatRole2 属性
        chatRole2.setName(user.getName());
        chatRole2.setAvatar(user.getAvatar());
        chatRole2.setRole(user.getRole());
        chatRole2.setUsersId(user.getId());
        // 检查 chatRole2 是否存在
        ChatRole existingRole2 = chatRoleMapper.selectByUserId(user.getId());
        if (existingRole2 == null) {
            chatRoleMapper.insert(chatRole2);
            existingRole2 = chatRoleMapper.selectByUserId(user.getId());
        }
        // 获取插入后的 ID
        Integer chatRoleId1 = existingRole1.getId(); // 从数据库获取的 ID
        Integer chatRoleId2 = existingRole2.getId(); // 从数据库获取的 ID
        // 返回 ID 的映射
        Map<String, Integer> resultMap = new HashMap<>();
        resultMap.put("chatRoleId1", chatRoleId1);
        resultMap.put("chatRoleId2", chatRoleId2);
        return resultMap;
    }



    /**
     * 删除
     */
    public void deleteById(Integer id) {
        chatRoleMapper.deleteById(id);
    }

    /**
     * 批量删除
     */
    public void deleteBatch(List<Integer> ids) {
        for (Integer id : ids) {
            chatRoleMapper.deleteById(id);
        }
    }

    /**
     * 修改
     */
    public void updateById(ChatRole chatRole) {
        chatRoleMapper.updateById(chatRole);
    }

    /**
     * 根据ID查询
     */
    public ChatRole selectById(Integer id) {
        return chatRoleMapper.selectById(id);
    }

    /**
     * 查询所有
     */
    public List<ChatRole> selectAll(ChatRole chatRole) {
        return chatRoleMapper.selectAll(chatRole);
    }

    /**
     * 分页查询
     */
    public PageInfo<ChatRole> selectPage(ChatRole chatRole, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<ChatRole> list = chatRoleMapper.selectAll(chatRole);
        return PageInfo.of(list);
    }

}
